package org.deri.feather.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IRIElement extends Element {

	private String namespace;
	private String readableName;
	
	public IRIElement(String stringValue) {
		super(stringValue, Element.IRI_TYPE);
		
		this.namespace = extractNamespace(stringValue);
		this.readableName = extractReadableName(stringValue);
	}
	
	public String getNamespace() {
		return this.namespace;
	}
	
	public String getReadableName() {
		return this.readableName;
	}
	
	public String getIri() {
		return this.stringValue;
	}
	
	private String extractNamespace(String iri) {
		String namespace = "";
		
		namespace = iri.substring(0, iri.lastIndexOf("/"));
		return namespace;
	}
	
	private String extractReadableName(String iri) {
		String name;
		
		name = iri.substring(iri.lastIndexOf("/")+1);
	      
		if(name.contains("#"))
	  		name = name.substring(name.lastIndexOf("#")+1, name.length());
	  	
	  	if(name.contains("_"))
	  		name = name.replace("_", " ");

	  	if(name.contains(":"))
	  		name = name.replace(":", "");
	  	
	  	if(name.contains("\""))
	  		name = name.replace("\"", "");
	  	
	  	Pattern p = Pattern.compile("\\p{Lu}");
	    Matcher m = p.matcher(name);
	    StringBuffer sb = new StringBuffer();
	    while (m.find()) {
	    	m.appendReplacement(sb, " " + m.group());
	    }
	    m.appendTail(sb);
	    name = sb.toString().trim();
		
	    name = name.replaceAll("[ ]+", " ");
	    
		return name;
	}

}
